package com.zt.weibo.detail;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zt.weibo.R;
import com.zt.weibo.WeiboApp;
import com.zt.weibo.interfaces.IWeiboPopListener;
import com.zt.weibo.interfaces.IWeiboServiceListener;
import com.zt.weibo.main.WeiboImgPopwin;
import com.zt.weibo.main.WeiboSendPopwin;
import com.zt.weibo.utils.AsyncImageLoader;
import com.zt.weibo.utils.FormatString;
import com.zt.weibo.utils.JsonAnalyze;
import com.zt.weibo.utils.PrintLog;
import com.zt.weibo.utils.SwitchActivity;
import com.zt.weibo.vo.EnumSendType;
import com.zt.weibo.vo.StatusInfo;

public class WeiboDetailActivity extends Activity
{
	private AsyncImageLoader mIconLoader = AsyncImageLoader.getInstance();
	private StatusInfo mStatusInfo = null;
	private ServerListener serverListener = null;
	private ImageView mUserImg = null;
	private TextView mUserName = null;
	private TextView mStatusContent = null;
	private LinearLayout mStatusImg = null;
	private LinearLayout mRetweetLayout = null;
	private TextView mRetUserName = null;
	private TextView mRetStatusContent = null;
	private LinearLayout mRetStatusImg = null;
	private TextView mRetStatusState = null;
	private Button mRepostBtn = null;
	private Button mCommentBtn = null;
	private TextView mStatusCreatTime = null;
	private TextView mStatusComeFrom = null;
	private TextView mRetBeenDelet = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		PrintLog.debug("WeiboDetailActivity On Create");
		setContentView(R.layout.weibo_detail);
		WeiboApp.addActivity(this);
		if(null != getIntent().getSerializableExtra("StatusInfo"))
		{
			mStatusInfo = (StatusInfo) getIntent().getSerializableExtra("StatusInfo");
			findView();
			initView();
			setListeners();
			serverListener = new ServerListener();
		}
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		PrintLog.debug("WeiboDetailActivity On Start");
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		PrintLog.debug("WeiboDetailActivity On Resume");
		WeiboApp.mServer.getStatusById(mStatusInfo.strId, serverListener);
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		PrintLog.debug("WeiboDetailActivity On Pause");
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		PrintLog.debug("WeiboDetailActivity On Stop");
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		PrintLog.debug("WeiboDetailActivity On Destroy");
		WeiboApp.delActivity(this);
	}
	
	private void findView()
	{
		mUserImg = (ImageView) findViewById(R.id.weibo_detail_user_img);
		mUserName = (TextView) findViewById(R.id.weibo_detail_user_text);
		mStatusContent = (TextView) findViewById(R.id.weibo_detail_status_text);
		mStatusImg = (LinearLayout) findViewById(R.id.weibo_detail_status_img);
		mRetweetLayout = (LinearLayout) findViewById(R.id.weibo_detail_retweet_layout);
		mRetUserName = (TextView) findViewById(R.id.weibo_detail_retweet_user_text);
		mRetStatusContent = (TextView) findViewById(R.id.weibo_detail_retweet_status_text);
		mRetStatusImg = (LinearLayout) findViewById(R.id.weibo_detail_retweet_status_img);
		mRetStatusState = (TextView) findViewById(R.id.weibo_detail_retweet_status_state_text);
		mRepostBtn = (Button) findViewById(R.id.weibo_detail_repost_btn);
		mCommentBtn = (Button) findViewById(R.id.weibo_detail_comment_btn);
		mStatusCreatTime = (TextView) findViewById(R.id.weibo_detail_status_time_text);
		mStatusComeFrom = (TextView) findViewById(R.id.weibo_detail_status_come_from_text);
		mRetBeenDelet = (TextView) findViewById(R.id.weibo_detail_retweet_status_delet_hint);
	}
	
	private void initView()
	{
		mIconLoader.loadImage(mStatusInfo.mUserInfo.strProfileImageUrl, mUserImg);
		mUserName.setText(FormatString.formatRemark(mStatusInfo.mUserInfo));
		mStatusContent.setText(mStatusInfo.strContent);
		mStatusImg.setVisibility(View.GONE);
		if(!"".equals(mStatusInfo.strBmiddlePic))
		{
			mStatusImg.setTag(mStatusInfo.strOriginalPic);
			((ImageView)mStatusImg.getChildAt(0)).setImageResource(R.drawable.icon);
			mIconLoader.loadImage(mStatusInfo.strBmiddlePic, mStatusImg);
		}
		mRepostBtn.setText(mStatusInfo.iRepostsCount + "");
		mCommentBtn.setText(mStatusInfo.iCommentsCount + "");
		mStatusCreatTime.setText(mStatusInfo.strCreatTime);
		mStatusComeFrom.setText(getString(R.string.come_from) + mStatusInfo.strSource);
		if(mStatusInfo.bIsRetweetOther)
		{
			if(1 == mStatusInfo.mRetweetInfo.iDeleted)
			{
				mRetBeenDelet.setVisibility(View.VISIBLE);
				return;
			}
			mRetweetLayout.setVisibility(View.VISIBLE);
			mRetUserName.setText("@" + FormatString.formatRemark(mStatusInfo.mRetweetInfo.mUserInfo));
			mRetStatusContent.setText(mStatusInfo.mRetweetInfo.strContent);
			mRetStatusImg.setVisibility(View.GONE);
			if(!"".equals(mStatusInfo.mRetweetInfo.strBmiddlePic))
			{
				mRetStatusImg.setTag(mStatusInfo.mRetweetInfo.strOriginalPic);
				((ImageView)mRetStatusImg.getChildAt(0)).setImageResource(R.drawable.icon);
				mIconLoader.loadImage(mStatusInfo.mRetweetInfo.strBmiddlePic, mRetStatusImg);
			}
			mRetStatusState.setText(String.format(getString(R.string.status_state), 
					mStatusInfo.mRetweetInfo.iRepostsCount, mStatusInfo.mRetweetInfo.iCommentsCount));
		}
	}
	
	private void setListeners()
	{
		mRepostBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
			{
				WeiboSendPopwin weiboSendPopwin = new WeiboSendPopwin(WeiboDetailActivity.this, 
						getLayoutInflater().inflate(R.layout.weibo_detail, null), new IWeiboPopListener() {
							
							@Override
							public void onPopShow()
							{
								
							}
							
							@Override
							public void onPopDismiss()
							{
								WeiboApp.mServer.getStatusById(mStatusInfo.strId, serverListener);
							}
						});
				weiboSendPopwin.setSendType(EnumSendType.E_SEND_REPOST, mStatusInfo);
				weiboSendPopwin.show();
			}
		});
		
		mCommentBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
			{
				SwitchActivity.weiboComment(WeiboDetailActivity.this, mStatusInfo);
			}
		});
		
		ClickListener clickListener = new ClickListener();
		mStatusImg.setOnClickListener(clickListener);
		mRetStatusImg.setOnClickListener(clickListener);
	}
	
	private class ClickListener implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			WeiboImgPopwin imgPopwin = new WeiboImgPopwin(WeiboDetailActivity.this, 
					getLayoutInflater().inflate(R.layout.weibo_main, null));
			imgPopwin.show((String)v.getTag());
		}
	}
	
	private class ServerListener implements IWeiboServiceListener
	{
		@Override
		public void onStart()
		{
			
		}

		@Override
		public void onCompl(String strJson)
		{
			JSONObject tempObject = null;
			try
			{
				tempObject = new JSONObject(strJson);
			}
			catch (JSONException e)
			{
				e.printStackTrace();
			}
			mStatusInfo = JsonAnalyze.getInstance().analyzeStatusInfo(tempObject);
			mRepostBtn.setText(mStatusInfo.iRepostsCount + "");
			mCommentBtn.setText(mStatusInfo.iCommentsCount + "");
		}

		@Override
		public void onError(String str)
		{
			StringBuffer strBuffer = new StringBuffer();
			strBuffer.append(str.substring(8, str.length()));
			PrintLog.debug(strBuffer.toString());
			int iErrorCode = Integer.parseInt(strBuffer.toString());
			switch(iErrorCode)
			{
			case 21327://token过期，跳转至登陆界面重新授权
				Toast.makeText(WeiboDetailActivity.this, getString(R.string.toast_token_invaled), 1000).show();
				WeiboApp.mHandler.postDelayed(new Runnable() {
					
					@Override
					public void run()
					{
						SwitchActivity.login(WeiboDetailActivity.this);
						finish();
					}
				}, 1200);
				break;
			}
		}
	}
	
}
